package com.saturn.action.sldb.emergency;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.saturn.app.utils.BeanUtils;
import com.saturn.app.web.IAction;
import com.saturn.app.web.IView;
import com.saturn.app.web.view.JspErrorView;
import com.saturn.app.web.view.JspView;
import com.saturn.sldb.Emergency;
import com.saturn.sldb.TempSub;

public class AddEmergencyAction implements IAction{
	@Override
	public IView execute(HttpServletRequest request,
			HttpServletResponse response) {
		// TODO Auto-generated method stub
		Emergency vo = BeanUtils.getBean(request, Emergency.class);
		
		String[] c_inputType = request.getParameterValues("c_inputType");
		String[] c_name = request.getParameterValues("c_name"); // 姓名
		String[] c_sex = request.getParameterValues("c_sex"); // 性别
		String[] c_personRelation = request.getParameterValues("c_relation"); // 与户主关系
		String[] c_identify = request.getParameterValues("c_identify"); // 身份证号
		String[] c_contact = request.getParameterValues("c_contact"); // 联系电话
		
		List<TempSub> list = new ArrayList<TempSub>();
		for (int i = 0; i < c_name.length; i++) {
			if (!"".equals(c_name[i]) || !"".equals(c_sex[i])
					|| !"".equals(c_personRelation[i])
					|| !"".equals(c_identify[i])
					|| !"".equals(c_contact[i])) {
				if("".equals(c_inputType[i])){
					c_inputType[i] = "手动";
				} 
				TempSub sub = new TempSub(null, c_identify[i],
						c_name[i], c_sex[i], c_contact[i],
						vo.getCreateTime(), vo.getCreater(),
						vo.getCreaterName(), vo.getCreaterDepartment(),
						vo.getState(), vo.getInputType(), null,
						c_personRelation[i], vo.getName());
				list.add(sub);
			}
		}
		String type = request.getParameter("type1");
		if (type != null && type.trim().equals("auto")) {
			if (Emergency.addAuto(vo,list) == 1) {
				return new JspView("/app/sldb/emergency/apply/autoShow.jsp");
			} else {
				return new JspErrorView("添加申报救助信息失败");
			}
		} else {
			if (Emergency.addAuto(vo,list) == 1) {
				return new JspView("/app/sldb/emergency/apply/show.jsp");
			} else {
				return new JspErrorView("添加申报救助信息失败");
			}
		}
	}
	@Override
	public String requestMapping() {
		return "/app/sldb/emergency/apply/addEmergency.action";
	}
}
